/**
 * AQ:底层通讯包，包含协议发送，转换
 */
var PUBLIC_FIELD = {};//公共字段

var AQ = {};

/**
 * 对传进来的数据进行打包
 * 目前只想到扩展公共字段，后期扩展方便
 * */
AQ.Pack = function (obj) {
    return $.extend(obj, PUBLIC_FIELD);
}

/**
 * @description 解析包
 * @param {String} d应答包
 * @return {Array} 返回json数组
 * for example: AQ.parse('[[0,"success",2],["prono","proname"],["099","rio鸡尾酒"],["023","金鱼"]]');
 * return [{"code":0,"info":"success","count":2},{"prono":"099","proname":"rio鸡尾酒"},{"prono":"023","proname":"金鱼"}]
 */
AQ.parse = function (d) {
    try {
        var res = JSON.parse(d);
        var obj = [];
        obj.push({
            "code": res[0][0],
            "info": res[0][1],
            "count": res[0][2]
        });
        if (res.length > 2) {
            for (var i = 2; i < res.length; ++i) {
                obj.push({});
                for (var k in res[i]) {
                    obj[i - 1][res[1][k]] = res[i][k];
                }
            }
        }
        return obj;
    } catch (e) {
        return [{"code": "-999", "info": "哎呀！数据解析出了点儿小状况。"+e.message}];
    }
};

/**
 * @description 发送请求
 * @param {String} fucname功能名，比如applyCaptchav.do
 * @param {Object} sendata发送的请求包,一个json对象
 * @param {Function} sucCallBack成功回调函数，出参json对象
 * @param {Function} failCallBack失败回调函数，出参错误信息和错误号
 */
AQ.callAQ = function (fucname, sendata, sucCallBack, failCallBack) {
	failCallBack = failCallBack || alert;
    $.ajax({
        url: "../feizuan-web/" + fucname,
        type: "POST",
        dataType: "text",
        contentType:"application/x-www-form-urlencoded;charset=utf-8",
        data: new AQ.Pack(sendata),
        timeout: 30000,
        success: function (data, textStatus) {
            var res = AQ.parse(data);
            if (res[0].code == 0) {
                sucCallBack(res);
            } else if(res[0].code == 88) {
				location.replace("login.html?redirectURL=" + window.location.href);
			} else if(res[0].code == 555) {
				Utils.msgDialog(res[0].info);
			} else failCallBack(res[0].info, res[0].code);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            failCallBack("服务器忙，请重试！"+textStatus, "-99");
        }
    });
};


/**
 * @description 发送请求,返回json类型数据
 * @param {String} fucname功能名，比如applyCaptchav.do
 * @param {Object} sendata发送的请求包,一个json对象
 * @param {Function} sucCallBack成功回调函数，出参json对象
 * @param {Function} failCallBack失败回调函数，出参错误信息和错误号
 */
AQ.callAQJSON = function (fucname, sendata, sucCallBack, failCallBack) {
    failCallBack = failCallBack || alert;
    $.ajax({
        url: "../feizuan-web/" + fucname,
        type: "POST",
        dataType: "json",
        contentType: "application/x-www-form-urlencoded;charset=utf-8",
        data: new AQ.Pack(sendata),
        timeout: 30000,
        success: function (data, textStatus) {
			if (data.status == 0) {
                sucCallBack(data);
            } else if(data.status == 88) {
				location.replace("login.html?redirectURL=" + window.location.href);
			} else if(data.status == 555) {
				Utils.msgDialog(data.info);
			} else failCallBack(data.info, data.status);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            failCallBack("服务器忙，请重试！"+textStatus, "-99");
        }
    });
};
